#!/bin/sh

TEST_DIR=''
cleanup() { [ -z "${TEST_DIR}" ] || command rm -rf "${TEST_DIR}"; }
die() { echo "$@" >&2; exit 1; }
trap 'cleanup' 0

unset NVM_ONLY NVM_ONLY_SELECTED
TEST_DIR="$(mktemp -d)" || die 'could not create temporary directory'
NVM_DIR="${TEST_DIR}"
export NVM_DIR

set -- --no-use
\. ../../../nvm.sh || die 'could not source nvm.sh'
set --
\. ../../common.sh || die 'could not source common.sh'

nvm_get_os() { nvm_echo linux; }
nvm_get_arch() { nvm_echo x64; }
nvm_has_system_node() { return 1; }
nvm_has_system_iojs() { return 1; }
nvm_download() {
  nvm_echo unexpected > "${TEST_DIR}/network-attempted"
  return 1
}

cache_archive() {
  local SLUG
  SLUG="$(nvm_get_download_slug node "$1" "$2")" || return $?
  local CACHE_SUBDIR
  case "$1" in binary) CACHE_SUBDIR=bin ;; source) CACHE_SUBDIR=src ;; esac
  command mkdir -p "$(nvm_cache_dir)/${CACHE_SUBDIR}/${SLUG}" \
    && nvm_echo placeholder > "$(nvm_cache_dir)/${CACHE_SUBDIR}/${SLUG}/${SLUG}.$(nvm_get_artifact_compression "$2")"
}

assert_offline_version() {
  local EXPECTED
  EXPECTED="$1"
  shift
  try nvm_offline_version "$@"
  [ "_${CAPTURED_STDOUT}" = "_${EXPECTED}" ] || die "nvm_offline_version $*: expected >${EXPECTED}<, got >${CAPTURED_STDOUT}<"
  [ "_${CAPTURED_EXIT_CODE}" = '_0' ] || die "nvm_offline_version $*: expected exit 0, got ${CAPTURED_EXIT_CODE}"
}

cache_archive source v22.11.0 || die 'could not cache source archive'
cache_archive binary v22.12.0 || die 'could not cache binary archive'
command mkdir -p "$(nvm_cache_dir)/src/node-v22.13.0" || die 'could not create incomplete cache entry'
nvm alias team 22 >/dev/null || die 'could not create alias'

# With nothing installed, take the newest cached version that meets the minimum and can be installed.
assert_offline_version v22.12.0 22 22.11 0 0
assert_offline_version v22.11.0 22 22.11 1 0
assert_offline_version v22.12.0 22 22.11 0 1
assert_offline_version v22.11.0 team 22.11 1 0
assert_offline_version v22.12.0 team 22.11 0 0
assert_offline_version v22.12.0 node 22.11 0 0
command mkdir -p "${NVM_DIR}/alias/lts" || die 'could not create lts alias directory'
nvm_echo v22.11.0 > "${NVM_DIR}/alias/lts/jod" || die 'could not create lts alias'
nvm_echo lts/jod > "${NVM_DIR}/alias/lts/*" || die 'could not create lts/* alias'
assert_offline_version v22.11.0 'lts/*' 22 0 0

# Without an installable candidate, keep the newest cached match, so the caller can report why it fails.
assert_offline_version v22.13.0 22 22.13
assert_offline_version v22.13.0 22 22.14
assert_offline_version v22.13.0 22 22.12 1 0
command mkdir -p "$(nvm_cache_dir)/src/iojs-v3.3.1" || die 'could not create incomplete io.js cache entry'
assert_offline_version iojs-v3.3.1 iojs 3

try nvm_offline_version 21 22 0 0
[ "_${CAPTURED_STDOUT}" = '_N/A' ] || die "nvm_offline_version 21: expected N/A, got >${CAPTURED_STDOUT}<"
[ "_${CAPTURED_EXIT_CODE}" = '_3' ] || die "nvm_offline_version 21: expected exit 3, got ${CAPTURED_EXIT_CODE}"

# Without a minimum, aliases do not resolve against the cache.
for PATTERN in team node 'lts/*'; do
  try nvm_offline_version "${PATTERN}" '' 0 0
  [ "_${CAPTURED_STDOUT}" = '_N/A' ] || die "nvm_offline_version ${PATTERN} without a minimum: expected N/A, got >${CAPTURED_STDOUT}<"
  [ "_${CAPTURED_EXIT_CODE}" = '_3' ] || die "nvm_offline_version ${PATTERN} without a minimum: expected exit 3, got ${CAPTURED_EXIT_CODE}"
done

# An installed match is preferred unless it is below the minimum.
make_fake_node v22.10.0 || die 'could not create installed version'
assert_offline_version v22.10.0 22
assert_offline_version v22.10.0 22 22.10 1 0
assert_offline_version v22.11.0 22 22.11 1 0
assert_offline_version v22.11.0 team 22.11 1 0
# Below-minimum cached versions do not replace the installed one the caller reports.
assert_offline_version v22.10.0 22 22.12 1 0
cache_archive source v22.14.0 || die 'could not cache newer source archive'
assert_offline_version v22.12.0 22 22.12 0 1
assert_offline_version v22.14.0 22 22.12 0 0

[ ! -e "${TEST_DIR}/network-attempted" ] || die 'unexpected network access'
